package com.svconnect.plugin.readers;

import java.util.HashMap;
import java.util.Map;

public class WSDLSerialization {

	private final Map<String,String> symbolMap = new HashMap<String,String>();
	
	public WSDLSerialization() {
		symbolMap.put("boolean", "Boolean");
		symbolMap.put("byte", "Byte");
		symbolMap.put("dateTime", "java.util.Date");
		symbolMap.put("decimal", "Double");
		symbolMap.put("double", "Double");
		symbolMap.put("float", "Float");
		symbolMap.put("int", "Integer");
		symbolMap.put("long", "Long");
		symbolMap.put("short", "Short");
		symbolMap.put("string", "String");
		symbolMap.put("unsignedByte", "Byte");
		symbolMap.put("unsignedInt", "Integer");
		symbolMap.put("unsignedLong", "Long");
		symbolMap.put("unsignedShort", "Short");
		symbolMap.put("char", "Character");
		symbolMap.put("guid", "java.util.UUID");
	}
	
	public String getJavaTypeName(String wsdlTypeName) {
		if (symbolMap.containsKey(wsdlTypeName))
			return symbolMap.get(wsdlTypeName);
		return wsdlTypeName;
	}
	
	public String getJavaTypeName(String wsdlTypeName, boolean nullable) {
		if (nullable || wsdlTypeName.equals("dateTime") || wsdlTypeName.equals("guid"))
			return this.getJavaTypeName(wsdlTypeName);
		else {
			if (wsdlTypeName.startsWith("unsigned")) {
				return wsdlTypeName.replace("unsigned", "");
			}
			return wsdlTypeName;
		}
	}
	
	
	public String getWsdlTypeName(String javaTypeName) {
		if (javaTypeName.startsWith("java.lang."))
			javaTypeName = javaTypeName.replace("java.lang.", "");
		
		if (symbolMap.containsValue(javaTypeName)) {
			for(String key : symbolMap.keySet()) {
				if (symbolMap.get(key).equals(javaTypeName))
					return key;
			}
		}
		
		String[] typeNameSplit = javaTypeName.split("\\.");
		return typeNameSplit[typeNameSplit.length-1];
	}
	
	public boolean isJavaSimpleType(String javaTypeName) {
		return javaTypeName.startsWith("java.lang.") || javaTypeName.equals("java.util.Date");
	}
	
}
